﻿Imports CRM.BusinessLayer
Imports Telerik.Windows.Controls
Imports CommonDialog
Imports CRM.CommonDialog

Partial Public Class frmCRMMDI1
    Dim formLogin As frmLogin
    Dim formCRMMainForm As frmCRMMainForm

    Private Sub fnLoadData()

        Dim cmn As New CommonFuncs
        Try
            'Security.GetUserLevel()
            cmn.ForceDeleteTempDir()
            If CommonFuncs.CreateDIR("c:\database") = True Then
                If CommonFuncs.CreateDIR("c:\database\TempFiles") = True Then

                    'If CommonFuncs.commandlinedrives("subst j: /d") = True Then
                    If CommonFuncs.commandlinedrives("subst j: c:\database\tempfiles") = False Then
                        CommonFuncs.DisplayMessage("The J: has not been created. Please contact IT", CommonDialog.CustomDialogIcons.Information, "J:")
                    End If
                    'End If

                Else
                    CommonFuncs.DisplayMessage("The tempfiles diretory is not created. Please contact IT", CommonDialog.CustomDialogIcons.Information, "Directory")
                End If
            Else
                CommonFuncs.DisplayMessage("The database diretory is not created. Please contact IT", CommonDialog.CustomDialogIcons.Information, "Directory")
            End If

            'cmn.GblFunc_Detect_Acrobat()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmCRMMDI1 : fnLoadData")
        Finally
            cmn = Nothing

        End Try
    End Sub

    Private Sub frmCRMMDI1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        Try
            If CommonFuncs.DisplayyesNoMessage("Are you sure to exit CRM?", CustomDialogIcons.Question, "Close CRM") = 1 Then
                Dim WriteLog As New WriteLog
                WriteLog.WriteToEventLog("CRM3 Application exited", , EventLogEntryType.Information)
                End
            Else
                e.Cancel = True
            End If
        Catch ex As Exception
        Finally
            'End
        End Try
    End Sub
    Private Sub frmCRMMDI1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Call fnLoadData()
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Dim tabItem As RadTabItem = New RadTabItem()
        tabItem.Header = "Main Form"

        ''''''''''''''''''''''''
        formCRMMainForm = New frmCRMMainForm
        tabItem.Content = formCRMMainForm
        AddHandler formCRMMainForm.ShowDynamicTab, AddressOf CreateDynamicTab
        '''''''''''''''''''''''''''''''''''''''
        tabControl.Items.Add(tabItem)
        tabControl.SelectedItem = tabItem
    End Sub
    Public Sub CreateDynamicTab(ByVal tabItemName As String)
        Dim formMainDisplay As frmCRMMainDisplay
        Dim formAddAccount As frmAddAccount
        Dim formDocumentStore As frmDocumentStore
        Dim formUnassignedcases As frmUnassignedCases
        Dim tabItem As RadTabItem = Nothing
        If tabControl.Items.Count <= 2 Then
            tabItem = New RadTabItem
        Else
            tabItem = CType(tabControl.Item(2), RadTabItem)
            tabControl.Items.Remove(tabItem)
            tabItem = New RadTabItem
        End If

        If tabItemName.Trim = "maindisplay" Then
            formMainDisplay = New frmCRMMainDisplay()
            tabItem.Content = formMainDisplay
            tabItem.Header = "Case Details"
            AddHandler formMainDisplay.ShowDynamicTab, AddressOf CreateDynamicTab
            AddHandler formMainDisplay.CloseDynamicTab, AddressOf RemoveDynamicTab
        ElseIf tabItemName.Trim = "addaccount" Then
            formAddAccount = New frmAddAccount()
            tabItem.Content = formAddAccount
            tabItem.Header = "Add Account"
            AddHandler formAddAccount.ShowDynamicTab, AddressOf CreateDynamicTab
            AddHandler formAddAccount.CloseDynamicTab, AddressOf RemoveDynamicTab
        ElseIf tabItemName.Trim = "document" Then
            formDocumentStore = New frmDocumentStore
            tabItem.Content = formDocumentStore
            tabItem.Header = "Document Store"
            AddHandler formDocumentStore.CloseDynamicTab, AddressOf RemoveDynamicTab
        ElseIf tabItemName.Trim = "unassigned" Then
            formUnassignedcases = New frmUnassignedCases
            formUnassignedcases.MinimizeLoadCasesLoops = True
            tabItem.Content = formUnassignedcases
            tabItem.Header = "Unassigned Cases"
            AddHandler formUnassignedcases.CloseDynamicTab, AddressOf RemoveDynamicTab
        End If
        tabControl.Items.Add(tabItem)
        tabControl.SelectedItem = tabItem
    End Sub
    Public Sub RemoveDynamicTab(ByVal tabItemName As String)
        Dim tabItem As RadTabItem = tabControl.Items(2)
        If Not tabItem Is Nothing Then
            If tabItemName.Trim = "maindisplay" Then
                formCRMMainForm.RefreshLists()
                formCRMMainForm.LoadGrid()
                tabControl.Items.Remove(tabItem)
                tabControl.SelectedIndex = 1
            ElseIf tabItemName.Trim = "unassigned" Then
                formCRMMainForm.LoadGrid()
                tabControl.Items.Remove(tabItem)
                tabControl.SelectedIndex = 1
            ElseIf tabItemName.Trim = "document" Or tabItemName.Trim = "addaccount" Then
                Me.CreateDynamicTab("maindisplay")
            End If
        End If

    End Sub
End Class
